/**
 * 自动化将 plugins/components 里面所有的组件导入到 import.ts 中
 */
const fs = require("fs").promises;
const { glob } = require("glob");
const { kebabToPascal } = require("./tools");

// 项目根目录
const paths = glob.globSync("plugins/components/*/");

const floders = paths.map((floder) => floder.split("\\")[2]);

let importContent = "";
let exportContent = "";

floders.map((comp) => {
  importContent =
    `import ${kebabToPascal(comp)} from "./${comp}";\r\n` + importContent;
  exportContent = `  ${kebabToPascal(comp)},\r\n` + exportContent;
});

const finalContent = `${importContent}
export {
${exportContent}}
`;

try {
  fs.writeFile("plugins/components/imports.ts", finalContent);
  console.log("import:comp 命令更新 imports.ts 文件内容成功");
} catch (err) {
  console.log("import:comp 命令更新 imports.ts 文件内容失败");
}
